<?php
/**
 * Created by PhpStorm.
 * 商家提现业务
 * User: caicm
 * Date: 2017/6/28
 * Time: 15:52
 */

namespace WMApi\Service;

use WMApi\Model\WmSellerAccountRecordModel;
use WMApi\Model\WmSellerBankCardModel;
use WMApi\Model\WmSellerFreezeRecordModel;
use WMApi\Model\WmSellerModel;
use WMApi\Model\WmSellerWithdrawRecordModel;

class SellerWithdrawService {

    /**
     * 获取商家金额信息
     * Created By caicm
     * @param int $seller_id
     * @return array
     */
    public function get_seller_account_money($seller_id=0){
        $seller_model = new WmSellerModel();
        $bank_model = new WmSellerBankCardModel();
        $bank_count = $bank_model->find_bank_card_count($seller_id);

        $seller_info = $seller_model->find_seller_info($seller_id);

        //提现协议
        $note = array(
            array(
                'title' => '1、标题',
                'content' => '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容'
            ),
            array(
                'title' => '2、标题',
                'content' => '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容'
            ),
            array(
                'title' => '3、标题',
                'content' => '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容'
            ),
            array(
                'title' => '4、标题',
                'content' => '内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容'
            ),
        );
        return array(
            'account_money' => $seller_info['account_money'],                           //账户余额
            'serv_charge_proportion' => C('seller_withdraw_serv_charge_proportion'),    //手续费比例
            'minimum_serv_chrge' => C('seller_withdraw_minimum_serv_chrge'),            //最低手续费
            'store_min_money' => C('store_min_money'),                                  //店铺保证金额最低余额
            'store_min_money_text' => '提现后，账户余额低于'.C('store_min_money').'元，店铺将是休息状态，是否继续',
            'min_money' => C('seller_withdraw_min_money'),                              //提现最小金额
            'max_money' => C('seller_withdraw_max_money'),                              //提现最大金额
            //'withdraw_password_is_null' => $seller_info['withdraw_password']?0:1,       //提现密码是否为空
            'type' => $bank_count ? ($seller_info['withdraw_password']?0:2) : 1,         //状态：1-未绑定银行卡 2-未设置提现密码
            'note' => $note
        );
    }

    /**
     * 商家提现业务
     * Created By caicm
     * @param int $seller_id 商家id
     * @param int $bank_card_id 银行卡id
     * @param int $withdraw_money 提现金额
     * @param string $withdraw_password 提现密码
     * @return bool|mixed
     */
    public function apply($seller_id=0, $bank_card_id=0, $withdraw_money=0, $withdraw_password=''){
        $seller_model = new WmSellerModel();
        $account_model = new WmSellerAccountRecordModel();
        $withraw_model = new WmSellerWithdrawRecordModel();
        $freeze_model = new WmSellerFreezeRecordModel();

        $seller_model->startTrans();

        //银行卡信息
        $card_info = $this->get_bank_card_info($seller_id, $bank_card_id);
        if(!$card_info){
            returnApiError('银行卡信息有误');
        }

        //手续费用
        $serv_charge = $this->get_serv_charge($withdraw_money);
        //总金额
        $total_money = $withdraw_money+$serv_charge;
        //提现单号
        $withdraw_sn = get_goods_sn('w');
        $record_note = "提现申请，提现单号：{$withdraw_sn}";

        //检查提现信息
        $this->check_withraw($seller_id, $withdraw_money, $total_money, $withdraw_password);

        /**
         * 修改商家账户余额
         */
        $update_account = $seller_model->dec_account_money($seller_id, $total_money);

        /**
         * 修改商家冻结账户金额
         */
        $update_freeze = $seller_model->add_freeze_money($seller_id, $total_money);

        /**
         * 写入提现操作记录
         */
        $add_withdraw = $withraw_model->add_record($seller_id, $withdraw_sn, $withdraw_money, $serv_charge, $total_money, $card_info);

        //商家信息
        $seller_info = $seller_model->find_seller_info($seller_id);

        /**
         * 写入余额变动记录
         */
        $add_account = $account_model->add_record($seller_id, 0, 1, $add_withdraw, 1, $total_money, $record_note, $seller_info['account_money']);

        /**
         * 写入冻结操作记录
         */
        $add_freeze = $freeze_model->add_record($seller_id, $add_withdraw, $total_money, $seller_info['account_money'], $record_note);

        if($update_account && $update_freeze && $add_withdraw && $add_account && $add_freeze){
            $seller_model->commit();
            return $add_withdraw;
        }else{
            $seller_model->rollback();
            return false;
        }

    }

    /**
     * 获取提现详情
     * Created By caicm
     * @param int $seller_id
     * @param int $withdraw_id
     * @return mixed
     */
    public function get_withdraw_info($seller_id=0, $withdraw_id=0){
        $withraw_model = new WmSellerWithdrawRecordModel();
        $withdraw_info = $withraw_model->find_record_info($seller_id, $withdraw_id);
        $withdraw_info['withdraw_time'] = get_date($withdraw_info['withdraw_time']);
        $withdraw_info['audit_time'] = get_date($withdraw_info['audit_time']);
        return $withdraw_info;
    }

    /**
     * 检查提现信息
     * Created By caicm
     * @param int $seller_id
     * @param int $withdraw_money
     * @param int $total_money
     * @param string $withdraw_password
     * @return bool
     */
    protected function check_withraw($seller_id=0, $withdraw_money=0, $total_money=0, $withdraw_password=''){
        $seller_model = new WmSellerModel();
        $set_service = new SellerSetService();
        $withdraw_model = new WmSellerWithdrawRecordModel();

        //商家信息
        $seller_info = $seller_model->find_seller_info($seller_id);
        $set_service->check_withdraw_password($withdraw_password, $seller_info['withdraw_password']);

        if($withdraw_money < C('seller_withdraw_min_money')){
            returnApiError('提现金额不能小于'.C('seller_withdraw_min_money').'元');
        }
        if($total_money > $seller_info['account_money']){
            returnApiError('账户余额不足'.$total_money);
        }
        if($withdraw_money > C('seller_withdraw_max_money')){
            returnApiError('单笔提现金额不能大于'.C('seller_withdraw_max_money').'元');
        }

        //获取当月申请数量
        if(C('seller_withdraw_month_most_num')){
            $withdraw_month_num = $withdraw_model->get_withdraw_month_num($seller_id);
            if($withdraw_month_num > C('seller_withdraw_month_most_num')){
                returnApiError('当月最多申请提现'.C('seller_withdraw_month_most_num').'次');
            }
        }

        //获取当月最新申请的信息
        if(C('seller_withdraw_min_interval')){
            $new_info = $withdraw_model->get_withdraw_month_new_info($seller_id);
            if($new_info){
                $info_time = $new_info['withdraw_time'] + C('seller_withdraw_min_interval');
                if($info_time > gmtime()){
                    $day = C('seller_withdraw_min_interval')/86400;
                    returnApiError($day.'天内不能再次申请');
                }

            }
        }


        return true;
    }



    /**
     * 获取提现手续费
     * Created By caicm
     * @param int $withdraw_money 提现金额
     * @return float|int|mixed
     */
    protected function get_serv_charge($withdraw_money=0){
        //商家提现手续费比例
        $serv_charge_proportion = C('seller_withdraw_serv_charge_proportion');
        if($serv_charge_proportion !=0 ){
            //最低费用
            $minimum_serv_chrge = C('seller_withdraw_minimum_serv_chrge');
            $serv_charge = ceil($withdraw_money*$serv_charge_proportion*100)/100;
            if($serv_charge < $minimum_serv_chrge){
                $serv_charge = $minimum_serv_chrge;
            }
        }else{
            $serv_charge = 0;
        }

        return $serv_charge;
    }

    /**
     * 获取区域列表
     * Created By caicm
     * @return mixed
     */
    public function get_region_list(){
        $region_service = new RegionService();
        return $region_service->get_region_list();
    }

    /**
     * 添加银行卡
     * Created By caicm
     * @param int $seller_id
     * @param string $bank_card_account
     * @param string $bank_card_number
     * @param string $bank_card_dbk
     * @param string $bank_card_subbranch
     * @param int $province_id
     * @param int $city_id
     * @param int $district_id
     * @return mixed
     */
    public function add_bank_card($seller_id=0, $bank_card_account='', $bank_card_number='', $bank_card_dbk='', $bank_card_subbranch='', $province_id=0, $city_id=0, $district_id=0){
        $region_service = new RegionService();
        $card_model = new WmSellerBankCardModel();
        //获取地址信息
        $address_data = $region_service->get_address_data($province_id, $city_id, $district_id);

        return $card_model->add_bank_card($seller_id, $bank_card_account, $bank_card_number, $bank_card_dbk, $bank_card_subbranch, $address_data);

    }

    /**
     * 获取商家银行卡列表
     * Created By caicm
     * @param int $seller_id
     * @return mixed
     */
    public function get_bank_card_list($seller_id=0){
        $card_model = new WmSellerBankCardModel();
        return $card_model->find_bank_card_list($seller_id);
    }

    /**
     * 获取银行卡详情
     * Created By caicm
     * @param int $seller_id
     * @param int $bank_card_id
     * @return mixed
     */
    public function get_bank_card_info($seller_id=0, $bank_card_id=0){
        $card_model = new WmSellerBankCardModel();
        return $card_model->find_bank_card_info($seller_id, $bank_card_id);
    }

    /**
     * 检查商家银行卡数量
     * Created By caicm
     * @param int $seller_id
     * @return bool
     */
    public function check_bank_card_num($seller_id=0){
        $card_model = new WmSellerBankCardModel();
        $card_num = $card_model->find_bank_card_count($seller_id);
        if($card_num >= C('seller_max_bank_card')){
            returnApiError('最多只能添加'.C('seller_max_bank_card').'张银行卡');
        }
        return true;
    }


}